package cn.edu.zjgsu.ito.controller;

import cn.edu.zjgsu.ito.interceptor.AdminControllerInterceptor;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashSet;
import java.util.List;

import cn.edu.zjgsu.ito.model.Course;
import cn.edu.zjgsu.ito.service.AdminCouManageService;
import cn.edu.zjgsu.ito.service.CollegeCodeNameCourseService;
import cn.edu.zjgsu.ito.service.ServiceFactory;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;

@Before(AdminControllerInterceptor.class)
public class AdminCouManageController extends Controller {

	AdminCouManageService adminCouManageService=ServiceFactory.createAdminCouManageService();
	private CollegeCodeNameCourseService collegeCodeNameCourseService = ServiceFactory.createCollegeCodeNameCourseService();
	private List<Record> Record1;
	/**
	 * 课程管理初始化
	 * 黄凯航
	 */
	public void index()
	{
		List<Record> courses=Db.find("select * from course");
		List<Record> colleges=Db.find("select collegeCode from course");
		Record1=CollegeCodeNameCourseService.allCollegeCodeNameCourse();	//获取所有学院名称和学院编号
		setAttr("allcollegeCodeNameCourse",Record1);
		HashSet<Record> c=new HashSet<Record>(colleges);
		colleges.clear();
		colleges.addAll(c);
		setAttr("collegeCode",Db.find("select * from college"));
		setAttr("college",colleges);
		setAttr("course",courses);
		render("coursemanage.html");
	}
	/**
	 * 添加课程跳转页面
	 * 黄凯航
	 */
	public void add()
	{
		render("addcourse.html");
	}
	/**
	 * 新课程添加
	 * 黄凯航
	 */
	public void submit()
	{
		Course course=getModel(Course.class,"course");
		String error=adminCouManageService.addSingleCourse(course);
		setAttr("error",error);
		index();
	}
	/**
	 * 编辑课程跳转页面
	 * 黄凯航
	 */
	public void edit()
	{
		Integer id=getParaToInt(0);
		if(id!=null&&id>0)
			setAttr("course",Course.dao.findById(id));
		render("correctcourse.html");
	}
	/**
	 * 课程更新
	 * 黄凯航
	 */
	public void update()
	{
		Course course = getModel(Course.class,"course");
		String error=adminCouManageService.updateSingleCourse(course);
		setAttr("error",error);
		index();
	}
	/**
	 * 异步课程更新
	 * 黄凯航
	 */
	public void ajaxupdate()
	{
		String courseName=getPara("courseName");
		int credit=getParaToInt("credit");
		int period=getParaToInt("period");
		String courseType=getPara("courseType");
		String collegeCode=getPara("collegeCode");
		String introduction=getPara("introduction");
		int id=getParaToInt("id");
		String courseNo=getPara("courseNo");

		Course course = getModel(Course.class);
		course.set("courseName", courseName);
		course.set("credit", credit);
		course.set("period", period);
		course.set("courseType", courseType);
		course.set("collegeCode", collegeCode);
		course.set("introduction", introduction);
		course.set("id", id);
		course.set("courseNo", courseNo);

		String error=adminCouManageService.updateSingleCourse(course);
		setAttr("course",Course.dao.find("select * from course where id=?",id));
		setAttr("error",error);
		renderJson();
	}
	/**
	 * 课程查找
	 * 黄凯航
	 */
	public void search()
	{
		String keyword=getPara("keyword").trim();
		List<Record> courses=Db.find("select * from course where courseNo like ?","%"+keyword+"%");

		courses.addAll(Db.find("select * from course where courseName like ?","%"+keyword+"%"));		
		HashSet<Record> c=new HashSet<Record>(courses);
		courses.clear();
		courses.addAll(c);

		setAttr("course",courses);

		List<Record> colleges=Db.find("select college from course");
		c=new HashSet<Record>(colleges);
		colleges.clear();
		colleges.addAll(c);

		setAttr("college",colleges);		
		render("coursemanage.html");
	}
	/**
	 * 异步课程查找
	 * 黄凯航
	 */
	public void ajaxsearch()
	{		
		String keyword=getPara("keyword");
		List<Course> course=Course.dao.find("select * from course where courseNo like ?","%"+keyword+"%");
		course.addAll(Course.dao.find("select * from course where courseName like ?","%"+keyword+"%"));

		HashSet<Course> c=new HashSet<Course>(course);
		course.clear();
		course.addAll(c);

		renderJson(course);	
	}
	/**
	 * 课程筛选
	 * 黄凯航
	 */
	public void screen()
	{
		String keyword=getPara("keywords");

		if(keyword.equals("all"))
			index();
		else
		{
			List<Record> courses=Db.find("select * from course where college=?",keyword);
			setAttr("course",courses);

			List<Record> colleges=Db.find("select collegeName from course");
			HashSet<Record> c=new HashSet<Record>(colleges);
			colleges.clear();
			colleges.addAll(c);

			setAttr("college",colleges);
			render("coursemanage.html");
		}
	}	
	/**
	 * 异步课程筛选
	 * 黄凯航
	 */
	public void ajaxscreen()
	{
		String keyword=getPara("keywords");

		if(keyword.equals("all"))
		{
			List<Course> course=Course.dao.find("select * from course");
			renderJson(course);
		}

		else
		{
			List<Course> course=Course.dao.find("select * from course where collegeCode=?",keyword);
			renderJson(course);
		}
	}
	/**
	 * 课程批量删除
	 * 黄凯航
	 */
	public void batch()
	{
		try{
			int id=0;
			String courseNo="";
			Record courseCourseNo=new Record();
			//String[] couCheckboxs=getParaValues("couCheckbox");
            String checkboxValue=getPara("checkboxValue");
            String[] couCheckboxs=checkboxValue.split(",");
            for(int i=0;i<couCheckboxs.length;i++)
            	System.out.print(i+"="+couCheckboxs[i]+"  ");
			for(int i=0;i<couCheckboxs.length;i++){
				id=Integer.parseInt(couCheckboxs[i]);
				courseCourseNo=Db.findById("course", id);
				courseNo=courseCourseNo.get("courseNo");
				adminCouManageService.deleteOrNot(courseNo);
				courseCourseNo=new Record();
			}
			redirect("/admincoumanage");
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			redirect("/admincoumanage");
		}
	}
	/**
	 * 课程模板下载方法
	 * 黄凯航
	 */
	public void fileDownload()
	{
		String fileName="课程模板.xls";
		String path=JFinal.me().getConstants().getFileRenderPath();
		path=path+"/"+fileName;
		renderFile(new File(path));
	}
	/**
	 * 课程模板上传方法
	 * 黄凯航
	 */
	public void fileUpload()
	{
		String filedir=PathKit.getWebRootPath();
		filedir=filedir+"\\WEB-INF\\upload";
		try
		{
			UploadFile file=getFile("uploadFile");
			String fileName=file.getFileName();
			String extension=fileName.substring(fileName.lastIndexOf("."));

			if(extension.equals(".xls")||extension.equals(".XLS"))
			{
				File source=file.getFile();
				String newFileName=System.currentTimeMillis()+extension;
				FileInputStream fis=new FileInputStream(source);
				File targetDir=new File(filedir);
				File target=new File(targetDir,newFileName);
				FileOutputStream fos=new FileOutputStream(target);
				byte[] bts=new byte[300];
				while (fis.read(bts,0,300)!=-1){
					fos.write(bts,0,300);
				}
				fos.close();
				fis.close();
				source.delete();
				String error=adminCouManageService.readCourseTemplate(fileName, newFileName, filedir);
				setAttr("error",error);
				index();
			}
			else
			{
				setAttr("error","文件类型错误");
				index();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
